/**
 * Created by gongxun on 16/12/12.
 */

var WhmjPlayerModel = function(){
    var that = {};

    that.seat = null;

    that.inhandCards = [];

    that.dealCards = [];

    that.openCards = [];

    that.singCards = [];

    that.isFromCard = false;

    //当前番数
    that.fanshu = 0;

    //是否开口
    that.hasOpened = false;

    //清空
    that.reset = function(){
        that.inhandCards = [];
        that.dealCards = [];
        that.openCards = [];
        that.singCards = [];
        that.isFromCard = false;
        that.fanshu = 0;
        that.hasOpened = false;
    };

    //重新排列手牌
    that.resetPlayer = function(){
        //inhand
        mo.whmjCardHelper.sortCardForNum(that.inhandCards);
        for (var i = 0; i < that.inhandCards.length; i++){
            var card = that.inhandCards[i];
            card.setCardKind(that.seat, 1);
            var p = mo.whmjCardHelper.getInhandCardPos(that.seat, i, that.openCards.length, (that.isFromCard && (i == that.inhandCards.length - 1)));
            card.setPosition(p);
        }

        //deal
        for (var i = 0; i < that.dealCards.length; i++){
            var card = that.dealCards[i];
            card.setCardKind(that.seat, 2);
            var p = mo.whmjCardHelper.getDealCardPos(that.seat, i);
            card.setPosition(p);
        }

        //open
        that.setOpenCard();

        //sing
        that.setSingCard();
    };

    that.addSingCard = function (card) {

        var hasSing = false;
        for (var i = 0; i < this.singCards.length; i++){
            var singCardStruct = this.singCards[i];

            var singCard = singCardStruct.card;
            if(singCard.mCardNum == card.mCardNum){
                hasSing = true;
                this.singCards[i].cardCount += 1;
            }
        }

        if(hasSing == false){
            var singCardStruct = {};
            singCardStruct.card = card;
            singCardStruct.cardCount = 1;
            this.singCards.push(singCardStruct);
        }else{
            card.removeFromParent();
        }
    },

    that.setSingCard = function () {
        for (var i = 0; i < this.singCards.length; i++){
            var singCardStruct = this.singCards[i];

            var singCard = singCardStruct.card;
            var singCardCount = singCardStruct.cardCount;

            singCard.setCardKind(that.seat, 2);
            singCard.setSingCardCount(that.seat, singCardCount);

            var p = mo.whmjCardHelper.getSingCardPos(that.seat, i);
            singCard.stopAllActions();
            singCard.setPosition(p);

        }

        that.sortInahndCard();
    },

    //排列开口牌
    that.setOpenCard = function(){
        that.isFromCard = false;
        for (var i = 0; i < that.openCards.length; i++){
            var cards = that.openCards[i].cards;
            var type = that.openCards[i].type;
            for (var j = 0; j < cards.length; j++){
                var card = cards[j];
                var num = card.mCardNum;

                if (type == 5){
                    if (j != 3){
                        card.setCardKind(that.seat, 4);
                    }else{
                        card.setCardKind(that.seat, num == 0 ? 4 : 3);
                    }
                }else{
                    card.setCardKind(that.seat, 3);
                }

                var p = mo.whmjCardHelper.getOpenCardPos(that.seat, i, j);
                card.stopAllActions();
                card.setPosition(p);

                if (j == 3){
                    card.mCardZOrder = mo.curSize.y;
                }
            }
        }

        that.sortInahndCard();
    };

    //强制排序
    that.sortInahndCard = function(){
        that.isFromCard = false;
        that.selectInhandCardIndex = null;
        if (that.inhandCards.length == 0){
            return;
        }

        mo.whmjCardHelper.sortCardForNum(that.inhandCards);
        for (var i = 0; i < that.inhandCards.length; i++){
            var card = that.inhandCards[i];
            card.stopAllActions();

            var p = mo.whmjCardHelper.getInhandCardPos(that.seat, i, that.openCards.length, false);
            card.setRotation(0);
            card.setPosition(p);
        }
    };

    //发牌动画
    that.setInhandCards = function(cards){
        that.isFromCard = (cards.length == 14);
        that.inhandCards = cards;
        mo.whmjCardHelper.sortCardForNum(that.inhandCards);

        for (var i = 0; i < that.inhandCards.length; i++) {
            var card = that.inhandCards[i];
            card.setCardKind(that.seat, 1);
            var p = mo.whmjCardHelper.getInhandCardPos(that.seat, i, that.openCards.length, (that.isFromCard && (i == that.inhandCards.length - 1)));
            card.setPosition(p);
            card.setOpacity(0);
            card.runAction(cc.sequence(
                cc.delayTime(0.05 * i),
                cc.callFunc(function(){
                    this.setOpacity(255);
                }, card)
            ));
        }
    };


    //添加手牌
    that.addInhandCard = function(card){
        that.isFromCard = true;
        that.inhandCards.push(card);

        card.setCardKind(that.seat, 1);
        var index = that.inhandCards.length - 1;
        var p = mo.whmjCardHelper.getInhandCardPos(that.seat, index, that.openCards.length, (that.isFromCard && (index == that.inhandCards.length - 1)));
        card.setPosition(p);
    };



    //打出手牌
    that.setDealCard = function(cardNum){
        that.isFromCard = false;
        var card = null;

        //先从站着的牌里面找
        if (that.selectInhandCardIndex){
            if (that.selectInhandCardIndex < that.inhandCards.length){
                var inhand = that.selectInhandCardIndex;
                if (that.inhandCards[inhand].mCardNum == cardNum){
                    card = that.inhandCards[inhand];
                    that.inhandCards.splice(inhand, 1);
                }
            }
        }

        //站着的牌没找到 从手牌里面找
        if (card == null){
            //倒着倒
            for (var i = that.inhandCards.length - 1; i >= 0; i--){
                if (that.inhandCards[i].mCardNum == cardNum){
                    card = that.inhandCards[i];
                    that.inhandCards.splice(i, 1);
                    break;
                }
            }
        }

        //手牌里面没找到 就拿末尾的
        if(card == null){
            var index = that.inhandCards.length - 1;
            card = that.inhandCards[index];
            that.inhandCards.splice(index, 1);
        }

        if (card){
            //找到了
            if (mo.whmjCardHelper.getIsSingCard(cardNum)){
                that.singCards.push(cardNum);
            }else{
                that.dealCards.push(card);
            }

            card.setCardNum(cardNum);
            card.setCardKind(that.seat, 2);
            var p = mo.whmjCardHelper.getDealCardPos(that.seat, that.dealCards.length - 1);
            card.stopAllActions();
            card.setOpacity(255);
            card.runAction(cc.sequence(
                cc.moveTo(0.2, p)
            ));
        }

        that.setDealCardAction();
    };


    //飞牌排序
    that.setDealCardAction = function(){
        that.selectInhandCardIndex = null;
        if (that.inhandCards.length == 0){
            return;
        }

        if (that.seat == 0){
            //飞牌动画
            var flyCard = null;
            var tempCards = [];
            for (var i = 0; i < that.inhandCards.length; i++){
                tempCards.push(that.inhandCards[i]);
            }
            mo.whmjCardHelper.sortCardForNum(that.inhandCards);
            if (tempCards[tempCards.length - 1] != that.inhandCards[that.inhandCards.length - 1]){
                flyCard = tempCards[tempCards.length - 1];
            }


            var time = 0.3;
            var time1 = time / 2;
            for (var i = 0; i < that.inhandCards.length; i++){
                var card = that.inhandCards[i];
                card.stopAllActions();
                card.setOpacity(255);
                var endPos = mo.whmjCardHelper.getInhandCardPos(that.seat, i, that.openCards.length, false);
                if (card == flyCard){
                    var rotate1 = 30;
                    var rotate2 = 0;
                    var cardHeight = 111 * mo.whmjCardHelper.inhandCardScale;
                    // card.runAction(cc.sequence(
                    //     cc.spawn(cc.rotateTo(time, rotate1), cc.moveBy(time, cc.p(0, cardHeight + 10))),
                    //     cc.moveTo(time, cc.p(endPos.x, cardHeight + 10)),
                    //     cc.spawn(cc.rotateTo(time, rotate2), cc.moveTo(time, endPos))
                    // ));
                    card.runAction(cc.sequence(
                        cc.moveBy(time, cc.p(0, cardHeight + 10)),
                        cc.moveTo(time, cc.p(endPos.x, cardHeight + 10)),
                        cc.moveTo(time, endPos)
                    ));
                }else{
                    card.setRotation(0);
                    card.runAction(cc.sequence(cc.moveTo(time * 2, endPos)));
                }
            }

        }else{
            that.sortInahndCard();
        }
    };

    //获取所有已经单杠了的牌的数量
    that.getSingCardsNum =  function () {

        var singCardCount = [];

        for (var i=0; i<3; i++){
            var count = 0;
            var tmpNum = 0;

            if (i == 0){
                tmpNum = mo.whmjCardHelper.laiziNum;
            }else if(i == 1){
                tmpNum = 86;
            }else {
                tmpNum = 85;
            }

            for (var j=0; j<this.singCards.length; j++){
                if (tmpNum == this.singCards[j]){
                    count++;
                }
            }
            singCardCount[i] = count;
        }
        
        return singCardCount;
    };


    //openType
    //1 吃 3张
    //2 碰 3张
    //4 明杠 4张
    //5 暗杠 4张
    //6 补杠 1张
    //7 单杠 1张
    that.updateFanshu = function (openType, cardNums) {

        if(mo.whmjTabel.mTableConfig.openrate == 1){
            //开口翻
            if(openType != 7){
                if(that.hasOpened == false){
                    that.hasOpened = true;
                    that.fanshu++;
                }

                if(openType == 4){
                    that.fanshu++;
                }else if (openType == 5){
                    that.fanshu += 2;
                }else if(openType == 6){
                    that.hasOpened = true;
                    that.fanshu++;
                }
            }else{

                if(mo.whmjCardHelper.getIsLaizi(cardNums[0])){
                    that.fanshu += 2;
                }else{
                    that.fanshu++;
                }
            }


        }else{
            //口口翻
            if(openType != 7){
                that.fanshu++;

                if(openType == 4){
                    that.fanshu++;
                }else if (openType == 5){
                    that.fanshu += 2;
                }else if(openType == 6){
                    that.fanshu++;
                }
            }else{

                if(mo.whmjCardHelper.getIsLaizi(cardNums[0])){
                    that.fanshu += 2;
                }else{
                    that.fanshu++;
                }
            }
        }

    };

    return that;
};